package com.study.concurrent.completablefuture;

import java.util.concurrent.*;

/**
 * The type Future test.
 */
public class FutureTest {
    /**
     * The entry point of application.
     *
     * @param args the input arguments
     * @throws ExecutionException   the execution exception
     * @throws InterruptedException the interrupted exception
     * @throws TimeoutException     the timeout exception
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
//        Future提供并行异步的功能
//          多线程/有返回/异步
//        优点：线程池+future
//        缺点：
        simplyApplyFuture();

    }

    private static void simplyApplyFuture() throws ExecutionException, InterruptedException, TimeoutException {
        FutureTask future= new FutureTask(()->{
            TimeUnit.SECONDS.sleep(5);
            return "123";
        });
        Thread thread=new Thread(future);
        thread.start();
//        System.out.println(future.get());//会阻塞
//        System.out.println(future.get(3,TimeUnit.SECONDS));//等待三秒，没有结果报错TimeOut
        while(true){
            if (future.isDone()) {
                System.out.println("future.get() = " + future.get());
                return;
            }
        }
    }


}
